package com.stupidzhe.vbreview.service.impl;

import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.cache.TeacherCache;
import com.stupidzhe.vbreview.dao.TeacherMapper;
import com.stupidzhe.vbreview.domain.Teacher;
import com.stupidzhe.vbreview.service.TeacherBaseService;
import com.stupidzhe.vbreview.util.Md5Util;
import com.stupidzhe.vbreview.util.TimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class TeacherBaseServiceImpl implements TeacherBaseService {

    private final
    TeacherMapper teacherMapper;

    private final
    TeacherCache teacherCache;

    private final
    Pattern pattern = Pattern.compile("^[0-9A-Za-z][.-_0-9A-Za-z]*@[0-9A-Za-z]+(\\.[0-9A-Za-z]+)+$");

    public TeacherBaseServiceImpl(TeacherMapper teacherMapper, TeacherCache teacherCache) {
        this.teacherMapper = teacherMapper;
        this.teacherCache = teacherCache;
    }


    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ)
    public String login(String account, String password, HttpServletRequest request) {
        String md5Password = password + Md5Util.salt;
        md5Password = Md5Util.getPwd(md5Password);
        Teacher teacher = teacherMapper.selectByAccountForUpdate(account);
        if (null == teacher || !teacher.getTeacherPwd().equals(md5Password) || "0".equals(teacher.getStatus())) {
            return "";
        }
        try {
            teacher.setIpAddr(request.getRemoteAddr());
            teacher.setLastLoginTime(TimeUtil.getCurrentTime());
            teacherMapper.updateByPrimaryKey(teacher);
            String md5Account = Md5Util.getPwd(teacher.getTeacherNum() + Md5Util.salt);
            teacherCache.addTeacher(account, teacher);
            return md5Account;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        
    }

    @Override
    public Teacher getTeacherByNumber(String account) {
        Teacher teacher = teacherCache.getTeacher(account);
        if (teacher == null) {
            teacher = teacherMapper.selectByAccountForUpdate(account);
        }
        return teacher;
    }

    @Override
    public List<Teacher> getTeachers() {
        return teacherMapper.getAllTeachers();
    }

    @Override
    public ResultBean edit(Teacher teacher, String email) {
        if (!pattern.matcher(email).matches()) {
            return new ResultBean(false, "邮件格式出错");
        }
        try {
            teacher.setEmail(email);
            teacherMapper.updateByPrimaryKeySelective(teacher);
        } catch (Exception e) {
            return new ResultBean(false, "更新失败");
        }
        teacherCache.addTeacher(teacher.getTeacherNum(), teacher);
        return new ResultBean(true, "邮箱更新成功");
    }

    @Override
    public Teacher getTeacherByClassId(Integer classId) {
        return teacherMapper.selectByClassId(classId);
    }
}
